<template>
  <view class="perfect-auth">
	  <!-- 公共组件-每个页面必须引入 -->
	  <public-module></public-module>
    <view class="perfect-top">
      <text>请按以下流程进行认证！</text>
    </view>
    <view class="perfect-cname">
      <view class="perfect-update">
        <view class="perfect-update-image">
          <u-upload url="https://upload.qiniup.com"
            @afterRead="handleAfterRead" name="licensePicUrl" multiple :maxCount="1"
            style="width:150px;height: 90px;">
            <img :src='licensePicUrl' mode="widthFix" style="width: 150px;height: 90px;"></img>
          </u-upload>
		  <view class="perfect-update-font">
		    请上传营业照料
		  </view>
        </view>
        
      </view>
    </view>
    <view class="perfect-auth-content" v-if="creditCode !=''">
      <view class="content-top">
        <view class="row u-flex">
          <view class="nominal">
            公司名称
          </view>
          <view class="input">
            <input type="text" v-model="companyName" placeholder="上传营业执照自动识别" disabled style="width:100%" />
          </view>
        </view>
        <view class="row u-flex">
          <view class="nominal">
            统一信用码
          </view>
          <view class="input">
            <input type="text" v-model="creditCode" placeholder="上传营业执照自动识别" disabled style="width:100%" />
          </view>
        </view>
		<view class="row u-flex" v-if="isAuth == 1">
		  <view class="nominal">
		    负责人
		  </view>
		  <view class="input">
		    <input type="text" v-model="contactName" placeholder="上传营业执照自动识别" disabled style="width:100%" />
		  </view>
		</view>
		<view class="row u-flex" v-if="isAuth == 1">
		  <view class="nominal">
		    联系手机号
		  </view>
		  <view class="input">
		    <input type="text" v-model="mobile"  placeholder="上传营业执照自动识别" disabled style="width:100%" />
		  </view>
		</view>
      </view>
	  
	  <div v-if="isAuth == 1"
	  style="background: rgba(254,247,227,1);border-radius: 6px;font-size: 22rpx;color: #FF6339;padding:12rpx 50rpx;margin-top:20rpx;">
	      企业已认证通过，请申请关联认证。
	  </div>

    </view>
    <view class="perfect-auth-button" v-if="creditCode != ''">
      <view class="button-top" v-if="isAuth == 1">
        <u-button type="primary" shape="circle" @click="applyGl">申请关联</u-button>
      </view>
	  <view class="button-top" v-else>
	    <u-button type="primary" shape="circle" @click="editAddCompany">提交认证</u-button>
	  </view>
    </view>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
  import {
    imgCheck
  } from '@/plugins/upload/upload.js'
  import {
    getLicenseIdentify,
    editCompany,
  } from '@/config/api2.js'
   import {addGlCompany} from '@/config/api.js'
  import store from '@/store'
  const md5 = require('../../plugins/md5.min.js');
  export default {
    data() {
      return {
        companyId: 0,
        companyName: '',
        creditCode: '',
        licensePicUrl: 'https://pic.gxmore.com/FiqGX7sq-tf7AxckbXJHCroBW1Pv',
        isAuth : 0,
		contactName:'',
		mobile:'',
		legalPerson:''
      }
    },
    onLoad(e) {
      
	  
    },
    computed: {},
    methods: {
      handleAfterRead({
        file
      }) {
        uni.showLoading({
          title: '上传中',
        });
        
        console.log('file', file)
        imgCheck(file[0].url).then((res) => {
          if (res != '') {
            // 获取上传成功后的图片地址
            this.licensePicUrl = res;
            // 生成签名串
            const encryption = md5(res + "gxmore..!");
            this.getLicenseIdentifys(res, encryption)
          }
          uni.hideLoading()

        }).catch(() => {
          uni.hideLoading()
        })
      },
	  
	  //申请关联 提交后去我的认证
	  applyGl(){
		  addGlCompany({companyId:this.companyId}).then(()=>{
		  	uni.showToast({
		  	  title: '关联申请已提交',
		  	  duration: 1000,
		  	  icon: 'none'
		  	})
		  	uni.setStorage({
		  	  key: 'addGl',
		  	})
		  	setTimeout(()=>{
		  		uni.navigateBack();
		  	},1000)
		  	
		  })
	  },
      getLicenseIdentifys(data1, data2) {
        getLicenseIdentify({
          licensePicUrl: data1,
          encryption: data2
        }).then(res => {
          this.companyName = res.companyName
          this.creditCode = res.creditCode
		  this.contactName = res.contactName
		  this.isAuth = res.isAuth
		  this.companyId = res.companyId
		  this.legalPerson = res.legalPerson
		  if(this.isAuth == 1){
			  uni.showToast({
			    title: '您的企业信息已被认证通过，请按提示操作！',
			    icon: 'none'
			  });
		  }
        }).catch(()=>{
			this.companyName = ''
			this.creditCode = ''
			this.contactName = ''
			this.isAuth = 0
			this.companyId = ''
		})
      },
     
	 
      // 提交认证 
      editAddCompany() {
        const data = {
          companyId: this.companyId,
          companyName: this.companyName,
          creditCode: this.creditCode,
          licensePicUrl: this.licensePicUrl,
          legalPerson:this.legalPerson,
        }
        editCompany(data).then(res => {
          uni.setStorage({
            key: 'editAddCompany',
            data: {
              id: this.companyId
            }
          })
          uni.showToast({
            title: '提交成功！',
            icon: 'success'
          });
          store.dispatch('userInfo')
          uni.navigateBack()
        })
      },
     
	 
    },
  }
</script>

<style lang="scss" scoped>
  @import '@/style/wmj.scss';

  .perfect-auth-content {
    .row {
      width: 100%;
      padding: 0 24rpx;
      border-top: solid 1upx #eee;

      .nominal {
        width: 30%;
        height: 100upx;
        font-size: 30upx;
        text-align: left;
        line-height: 100upx;
      }

      .input {
        width: 100%;
        padding: 20upx 0;
        align-items: center;
        font-size: 30upx;
        text-align: right;

      }

    }
  }
</style>